<template>
  <el-form :model="ruleForm" style="width: 60%" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="编号">
      <el-input v-model="ruleForm.id" readonly></el-input>
    </el-form-item>
    <el-form-item label="书名">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="作者">
      <el-input v-model="ruleForm.author"></el-input>
    </el-form-item>
    <el-form-item label="发布者">
      <el-input v-model="ruleForm.publish"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
      <el-button @click="resetFormBook('ruleForm')">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
name: "UpdateBook",
  //数据处理,获取id
  data() {
    return {
      ruleForm: {
        id:'',
        name: '',
        author: '',
        publish: '',
      },
    }
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.put('http://localhost:8181/book/update', this.ruleForm).then(function (resp){
            if(resp.data == "success"){
              _this.$alert('《'+_this.ruleForm.name+'》修改成功！',"消息",
                  { confirmButtonText: '确定',
                    callback: action => {
                      _this.$router.push("/selectBook")
                    }})
            }else{
              return false;
            }
          })
        }
      });
    },
    resetFormBook(formName) {
      this.$router.push("/selectBook")
    }
  },
  created() {
    const _this = this
    axios.get('http://localhost:8181/book/findById/' + this.$route.query.id).then(function (resp){
      _this.ruleForm = resp.data
    })
  }
}

</script>

<style scoped>

</style>